<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 审批申请数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\ReviewRequestService;
use common\validate\ReviewRequest;
use common\validate\ReviewRequestNode;
use support\ErrorCode;
use think\Exception;
use think\Request;
use Webman\Stomp\Client;

class ReviewRequestController extends VerifyController
{
    /**
     * @var ReviewRequestService
     */
    private $reviewRequestService;

    public function __construct()
    {
        parent::__construct();
        $this->reviewRequestService = ReviewRequestService::getInstance();
    }


    /**
     * 我的待审核列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function myReviewList(Request $request)
    {
        $param = $request->param();
        $res = $this->reviewRequestService->myReviewList($param['param'], $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 发起审批请求
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $nodeValidate = new ReviewRequestNode();
        $nodeValidate->scene('MultiCreate');
        foreach ($param['data']['reviewers'] as $reviewer) {
            if (!$nodeValidate->check($reviewer)) {
                throw_strack_exception($nodeValidate->getError(), ErrorCode::REVIEW_MULTI_CREATE_PARAM, $reviewer);
            }
        }

        $res = $this->reviewRequestService->create($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 执行审核修改审核节点状态
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function review(Request $request): \think\Response
    {
        $param = $request->param();
        $data = $param['data'];
        $noteValidate = new ReviewRequest();
        $paramList = $data['param'] ?? [];

        $noteValidate->scene('ReviewRequestParam');
        foreach ($paramList as $param) {
            if (!$noteValidate->check($param)) {
                throw_strack_exception('param ' . $noteValidate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }

        $noteValidate->scene('ReviewRequestParamNote');
        $note = $data['note_item']['note'] ?? [];
        if (!empty($note) && !$noteValidate->check($note)) {
            throw_strack_exception('note_item.note ' . $noteValidate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
        }


        try {
            $res = $this->reviewRequestService->review($data, $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 执行审核修改审核节点状态
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function batchReview(Request $request)
    {
        $data = $request->param()['data'];
        $reviewValidate = new ReviewRequest();
        $reviewValidate->scene('BatchReviews');
        foreach ($data as $param) {
            if (!$reviewValidate->check($param)) {
                throw_strack_exception('param ' . $reviewValidate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->reviewRequestService->batchReview($data, $request->getUserId());
        return json(success_response('', $res));
    }

    /**
     * 我提交的审核列表
     * @param Request $request
     * @return \think\Response
     */
    public function selectMySubmittedReviewList(Request $request)
    {
        $res = $this->reviewRequestService->selectMySubmittedReviewList($request->param());
        return json(success_response('', $res));
    }

    /**
     * 我的待审核列表
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function reviewList(Request $request): \think\Response
    {
        $param = $request->param();
        $res = $this->reviewRequestService->reviewList($param['param'], $request->getUserId());
        return json(success_response('', $res));
    }

    /**
     * 我的待审核列表
     * @param Request $request
     * @return \think\Response
     */
    public function myReviewListGroupByStepCategory(Request $request)
    {
        $param = $request->param();
        if (!isset($param['param']['page'])) {
            $param['param']['page'] = [1, C("database.database_max_select_rows")];
        }
        $res = $this->reviewRequestService->myReviewListGroupByStepCategory($param['param'], $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 查询审核点审批结果
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getReviewResult(Request $request)
    {
        $param = $request->param();
        $res = $this->reviewRequestService->getReviewResult($param['param']);
        return json(success_response('', $res));
    }

    /**
     * 我的待审核返修单列表
     * @param Request $request
     * @return \think\Response
     */
    public function taskRepairOrderReviewList(Request $request): \think\Response
    {
        $param = $request->param();
        $param = $param['param'] ?? [];
        try {
            $res = $this->reviewRequestService->taskRepairOrderReviewList($param, $request->getUserId());
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
        return json(success_response('', $res));
    }

    /**
     * 批量创建
     * @param \think\Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function batchCreate(Request $request)
    {
        $param = $request->param();
        $nodeValidate = new ReviewRequestNode();

        foreach ($param['data'] as $dataItem) {
            $nodeValidate->scene('BatchCreateItem');
            if (!$nodeValidate->check($dataItem)) {
                throw_strack_exception($nodeValidate->getError(), ErrorCode::REVIEW_MULTI_CREATE_PARAM, $dataItem);
            }

            $nodeValidate->scene('MultiCreate');
            foreach ($dataItem['reviewers'] as $reviewer) {
                if (!$nodeValidate->check($reviewer)) {
                    throw_strack_exception($nodeValidate->getError(), ErrorCode::REVIEW_MULTI_CREATE_PARAM, $reviewer);
                }
            }
        }


        try {
            $res = $this->reviewRequestService->batchCreate($param['data'], $request->getUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
